/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.reportes;

import DBMS.DataBaseManager;
import clases.Reporte;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

/**
 *
 * @author Gimbet
 */
public class DescargarReporte extends DownloadAction {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";
    private static final String name = "Descargar Reporte";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    protected StreamInfo getStreamInfo(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        DataBaseManager d = DataBaseManager.getInstance();
        Reporte reporte = (Reporte) form;
        System.out.println("Reporte ID" + reporte.getIdreporte());
        if (reporte.getIdreporte() == null) {
            return null;
        }
        reporte = d.consultarReporte(reporte);
        System.out.println("Pase el reporte");
        //Obtenemos el reporte
        reporte.setReporte(d.consultarElementosReporte(reporte));
        //Seleccionar path
        String path = guardarArchivo() + "/" + reporte.getNombre() + ".pdf"; //guardarArchivo();
        System.out.println("Intentando crear reporte en " + path);
        FileOutputStream f = new FileOutputStream(path);
        reporte.imprimirArchivoDoc(f);
        String contentType = "application/pdf";
        System.out.println(" ");
        File file = new File(path);
        return new FileStreamInfo(contentType, file);
    }

    public String guardarArchivo() {
        JFileChooser fC = new JFileChooser();
        fC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        String path = "";
        if (fC.showSaveDialog(fC) == 0) {
            path = fC.getSelectedFile().getAbsolutePath();
        }
        return path;
    }
}
